package sk.fiit.icp.welcome.view;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.request.resource.ContextRelativeResource;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.apache.wicket.util.value.ValueMap;

import sk.fiit.icp.components.SignInSession;
import sk.fiit.icp.data.Users;
import sk.fiit.icp.data.UsersDao;

public class RegistrationPanel extends Panel {

	@SpringBean(name = "usersDao")
	public UsersDao usersDao;

	public RegistrationPanel(String id) {
		super(id);
		add(new RegistrationForm("registrationForm"));
	}

	class RegistrationForm extends Form<Void> {

		private static final String FNAME = "fname";
		private static final String LNAME = "lname";
		private static final String EMAIL = "email";
		private static final String PASSWORD = "password";

		// Property model for form
		private final ValueMap properties = new ValueMap();

		public RegistrationForm(String id) {
			super(id);

			// Attach textfield components that edit properties map model
			add(new TextField<String>(FNAME, new PropertyModel<String>(properties, FNAME)));
			add(new TextField<String>(LNAME, new PropertyModel<String>(properties, LNAME)));
			add(new TextField<String>(EMAIL, new PropertyModel<String>(properties, EMAIL)));
			add(new PasswordTextField(PASSWORD, new PropertyModel<String>(properties, PASSWORD)));
			SubmitLink submitLink = new SubmitLink( "registerLink" );
			submitLink.add( new Image( "registerImage", new ContextRelativeResource( "/images/Page/ButtonRegister.png" ) ) );
			add( submitLink );
		}

		@Override
		public final void onSubmit() {

			//Save user registration
			Users user = new Users(getFName(), getLName(), getEmail(), getPassword());
			usersDao.persist(user);

			getPage().replace(new Label("feedback", "Name: " + user.getFname() + " " + getLName() + "\n Thank you for registration!"));
			SignInSession session = (SignInSession)getSession();

			// Sign the user in
			if ( session.signIn( getEmail(), getPassword() ) ) {
				if ( !continueToOriginalDestination() ) {
					setResponsePage( getApplication().getHomePage() );
				}
			} else {

				String errmsg = getString( "feedback", null, "Unable to sign you in" );
				// Register the error message with the feedback panel
				error( errmsg );
			}		}

		String getFName() {
			return properties.getString(FNAME);
		}

		String getLName() {
			return properties.getString(LNAME);
		}

		String getEmail() {
			return properties.getString(EMAIL);
		}

		String getPassword() {
			return properties.getString(PASSWORD);
		}

	}

}